//
//  ZQEmotionTarbar.m
//  柳州微博
//
//  Created by Apple_Lzzy45 on 16/11/10.
//  Copyright © 2016年 Apple_Lzzy40. All rights reserved.
//

#import "ZQEmotionTarbar.h"
#import "ZQButton.h"

@interface ZQEmotionTarbar ()

@property(strong,nonatomic)ZQButton *button;

@end

@implementation ZQEmotionTarbar
-(instancetype)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
        //创建按钮
        [self setupBtnWithTitle:@"最近"buttonType:ZQEmotionTarbarButtonTypeRecent];
         [self setupBtnWithTitle:@"默认"buttonType:ZQEmotionTarbarButtonTypeDefault];
         [self setupBtnWithTitle:@"emoji"buttonType:ZQEmotionTarbarButtonTypeEmoji];
         [self setupBtnWithTitle:@"lxh"buttonType:ZQEmotionTarbarButtonTypeLxh];
    }
    return self;
}
-(void)setupBtnWithTitle:(NSString *)title buttonType:(ZQEmotionTarbarButtonType)buttonType
{
    ZQButton * btn =[[ZQButton alloc]init];
    
    [btn setTitle:title forState:UIControlStateNormal];
    btn.tag = buttonType;
    
    //添加点击事件
    [btn addTarget:self action:@selector(didButton:) forControlEvents:UIControlEventTouchUpInside];
//    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
//    
    [self addSubview:btn];
    
    //设置默认状态
    if ([title isEqual:@"默认"]) {
        [self didButton:btn];
    }
    //加载背景图
    if (self.subviews.count == 1) {
        //常态图
        [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_left_normal"]forState:UIControlStateNormal];
        //高亮图
        [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_left_selected"]forState:UIControlStateSelected];
        
    }else if (self.subviews.count == 4)
    {
        //常态图
        [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_right_normal"]forState:UIControlStateNormal];
        //高亮图
        [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_right_selected"]forState:UIControlStateSelected];
    
    }else
    {
        //常态图
        [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_mid_normal"]forState:UIControlStateNormal];
        //高亮图
        [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_mid_selected"]forState:UIControlStateSelected];
    
    }
    

}
/**
 * 子控件布局
 */
-(void)layoutSubviews
{
    [super layoutSubviews];
    //设置所有按钮的frame
    NSUInteger count = self.subviews.count;
    CGFloat btnW = self.width /count;
    CGFloat btnH = self.height;
    for (NSUInteger i =0; i<count; i++) {
        UIButton *btn =self.subviews[i];
        btn.x =i*btnW;
        btn.y =0;
        btn.width =btnW;
        btn.height =btnH;
    }
    
}
-(void)didButton:(ZQButton *)button
{
    //实现单选按钮
    _button.selected = NO;
    button.selected = YES;
    _button = button;

    ZQEmotionTarbarButtonType Type  = (int) button.tag;

    //调用代理方法
    if ([self.degleate respondsToSelector:@selector(emotionTarBar:didSelectBtn:)]) {
        [self.degleate emotionTarBar:self didSelectBtn:Type];
    }
}
@end
